﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;


namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestBatchRunner
    {
//        private const string strControlPath = BaseDirectoryHelper.Unused + "\\control";
//        private const string strOutputPath = BaseDirectoryHelper.Unused + "\\output";
//        private StringBuilder strScriptResults = new StringBuilder();
        private System.IO.MemoryStream m_Stream = new System.IO.MemoryStream();
//        private ScriptRunner m_ScriptRunner;

//        //[TestFixtureSetUp]
//        public void Init()
//        {
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir(strOutputPath);
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            this.m_ScriptRunner = new ScriptRunner(oDatabase, false);
//            m_ScriptRunner.ProgressEventPublisher += this.EventReceived;
//        }


//        //[Test]
//        public void TestRunScriptUsingFile()
//        {

//            string controlFile = strControlPath + "\\TestRunScriptUsingFile.txt";
//            string outputFile = strOutputPath + "\\TestRunScriptUsingFile_Results.txt";

//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [AdventureWorks]
//                                GO
//                                select * from Person.Address;";

//            ScriptRunner oScriptRunner = new ScriptRunner(oDatabase, false);
//            oScriptRunner.ProgressEventPublisher += this.EventReceived;
            
//            oScriptRunner.RunScriptUsingFile("Query1", sqlScript);
//            oScriptRunner.Join();

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFile, false);
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.CompareFiles(controlFile, outputFile);

//       }

        private void ClearResultBuilder()
        {
            this.m_Stream.Close();
            this.m_Stream = new System.IO.MemoryStream();
        }

        private void EventReceived(object source, ProgressEventArgs args)
        {
            lock (this.m_Stream)
            {
                string strMessage = args.Message;
                strMessage += Environment.NewLine;
                System.Text.Encoding oEncoder = System.Text.Encoding.Default;
                this.m_Stream.Write(oEncoder.GetBytes(strMessage), 0, strMessage.Length);
            }
            //this.strScriptResults.AppendLine(args.Message);
            //System.Console.WriteLine(args.Percent + " " + args.Message);

        }

//        //[Test]
//        public void TestAbortThread()
//        {
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [PatternsDB]
//                                GO
//                                exec long_proc;";

//            ClearResultBuilder();

//            m_ScriptRunner.RunScriptUsingFile("Query1", sqlScript);
//            System.Threading.Thread.Sleep(3000);
//            m_ScriptRunner.Abort();
//            m_ScriptRunner.Join();

//            System.Text.Encoding oEncoder = System.Text.Encoding.Default;
//            lock (m_Stream)
//            {
//                string strResults = oEncoder.GetString(m_Stream.GetBuffer());

//                System.Console.Write(strResults);
//                Assert.IsTrue(strResults.Contains("Aborted"), "results invalid : " + strResults);
//            }

//        }

//        //[Test]
//        public void TestAbortThreadUsingStreams()
//        {
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [PatternsDB]
//                                GO
//                                exec long_proc;";

//            ClearResultBuilder();

//            m_ScriptRunner.RunScriptUsingStreams("Query1", sqlScript);
//            System.Threading.Thread.Sleep(3000);
//            m_ScriptRunner.Abort();
//            m_ScriptRunner.Join();

//            System.Text.Encoding oEncoder = System.Text.Encoding.Default;

//            string strResults = oEncoder.GetString(m_Stream.GetBuffer());
//            System.Console.Write(strResults);
//            Assert.IsTrue(strResults.Contains("Aborted"), "results invalid : " + strResults);
//        }


//        //[Test]
//        public void TestInvalidQueryFileMode()
//        {
//            string controlFile = strControlPath + "\\InvalidQueryFile.txt";
//            string outputFile = strOutputPath + "\\InvalidQueryFile_results.txt";
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [Invalid_Database_z12]
//                                GONOGO
//                                exec non_existant_proc;";

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFile, false);
//            oFileHelper.CompareFiles(controlFile, outputFile);

//        }


        
//        //[Test]
//        public void TestRunTwoScripts()
//        {
////            string controlFile = strControlPath + "\\InvalidQueryFile_TwoScripts.txt";
//            string outputFileFirstScript = strOutputPath + "\\FirstScript.txt";
//            string outputFileSecondScript = strOutputPath + "\\SecondScript.txt";

//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [Invalid_Database_z12]
//                                GONOGO
//                                exec non_existant_proc;";

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFileFirstScript,false);
//            oFileHelper.CompareFiles(strControlPath + "\\InvalidQueryFile_FirstScript.txt", outputFileFirstScript);

//            //Helper_CompareFiles(strControlPath + "\\InvalidQueryFile_FirstScript.txt", outputFileFirstScript);

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFileSecondScript, false);
//            //Helper_CompareFiles(strControlPath + "\\InvalidQueryFile_SecondScript.txt", outputFileSecondScript);
//            oFileHelper.CompareFiles(strControlPath + "\\InvalidQueryFile_SecondScript.txt", outputFileSecondScript);

//        }

//        [Test]
//        public void TestRunScriptUsingStreams()
//        {
//            string controlFile = strControlPath + "\\InvalidQuery.txt";
//            string outputFile = strOutputPath + "\\InvalidQueryUsingStreams_results.txt";
//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [Invalid_Database_z12]
//                                GONOGO
//                                exec non_existant_proc;";

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFile, true);
//            oFileHelper.CompareFiles(controlFile, outputFile);

////            Helper_CompareFiles(controlFile, outputFile);

//        }

//        //[Test]
//        public void TestRunScriptUsingStreamsLongQuery()
//        {
//            string controlFile = strControlPath + "\\TestRunScriptUsingStreams.txt";
//            string outputFile = strOutputPath + "\\TestRunScriptUsingStreams_Results.txt";

//            FileHelper oFileHelper = new FileHelper();
//            oFileHelper.DeleteFilesInDir("temp");
//            Database oDatabase = new Database("AdventureWorks", ".", "AdventureWorks", strControlPath);
//            string sqlScript = @"use [AdventureWorks]
//                                GO
//                                select top(1000)* from Person.Address;";

//            Helper_TestResultsToFile(oDatabase, sqlScript, outputFile, true);
//            oFileHelper.CompareFiles(controlFile, outputFile);

//        }


        

//        private void Helper_TestResultsToFile(Database oDatabase, string sqlScript, string outputFile
//            , bool useStreams)
//        {
//            ClearResultBuilder();

//            if (useStreams)
//                m_ScriptRunner.RunScriptUsingStreams("Query1", sqlScript);
//            else
//                m_ScriptRunner.RunScriptUsingFile("Query1", sqlScript);
//            m_ScriptRunner.Join();

//            lock (this.m_Stream)
//            {
//                using (System.IO.StreamWriter outputWriter = new System.IO.StreamWriter(outputFile))
//                {
//                    this.m_Stream.WriteTo(outputWriter.BaseStream);
//                }
//            }

//        }

        [Test]
        public void TestRunDirCommand()
        {
            ClearResultBuilder();
            string outputFile = BaseDirectoryHelper.TestFiles_output + "\\run_dir.bat.results.txt";
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.TestFiles_output);

            string strBatchFile = BaseDirectoryHelper.TestFiles_control_FileCompare + "\\testfiles\\run_dir.bat";

            string strBatchFileResults = BaseDirectoryHelper.TestFiles_control_FileCompare + "\\testfiles\\run_dir.bat.results.txt";
            BatchRunner oBatchRunner = new BatchRunner(outputFile,
                strBatchFile, false);
            oBatchRunner.AddArgument(BaseDirectoryHelper.TestFiles_control_FileCompare + "\\testfiles" );

            oBatchRunner.ProgressEventPublisher += this.EventReceived;
            oBatchRunner.RunUsingStreams();
            oBatchRunner.Join();

            lock (this.m_Stream)
            {
                using (System.IO.StreamWriter outputWriter = new System.IO.StreamWriter(outputFile))
                {
                    this.m_Stream.WriteTo(outputWriter.BaseStream);
                }
            }
            
            oFileHelper.CompareFiles(strBatchFileResults, outputFile);

        }

        
        //[Test]
        //public void TestRunBatchFile()
        //{

        //}
    }
}
